package com.focus.xyz.union.cases.frontend;

import com.focus.xyz.core.common.annotation.Task;
import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.union.action.KeyWordAction;
import com.focus.xyz.core.common.listeners.TC;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.core.utils.ProcessUtil;
import com.focus.xyz.union.data.ApiBean;
import com.focus.xyz.union.data.InsDataService;
import com.focus.xyz.union.data.WebSiteBean;
import com.focus.xyz.union.pages.frontend.FrontMenuList;
import com.focus.xyz.union.pages.frontend.MyUnion;
import com.focus.xyz.union.pages.frontend.forms.ApiForm;
import com.focus.xyz.union.pages.frontend.forms.WebSiteForm;
import com.focus.xyz.union.pages.frontend.tables.ApiTable;
import com.focus.xyz.union.pages.frontend.tables.WebSiteTable;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.*;

/**
 * Created by chenmeng on 2016/5/26.
 */
public class ChannelTest extends TC {
    private static transient Logger logger = LoggerFactory.getLogger(ChannelTest.class);

    private WebDriver driver;
    private KeyWordAction action;

    @BeforeClass
    public void beforeClass() throws Exception{
        KeyWordAction.DelUnionFrontCookie();
    }
    @Parameters({"nodeUrl","browser","userName","password"})
    @BeforeMethod(alwaysRun = true)
    public void setUp(String nodeUrl,String browser,String userName,String password) throws Exception {
        driver = DriverFactory.getDriver(nodeUrl).getBrowser(browser);
        super.init(driver);
        action = new KeyWordAction(driver);
        action.LoginFront(userName,password);
    }

    @Parameters({"browser"})
    @AfterMethod(alwaysRun = true)
    public void tearDown(String browser) {
        try{
//            super.quit();
            driver.quit();
        }catch (Exception e){
            e.printStackTrace();
            ProcessUtil.closeBrowser(browser);
        }finally {

        }
    }

    @Task(taskName = {"DEL_WEBSITE_CHANNEL.sql"},paras = {"param0=cimon001"})
    @Parameters({"idx"})
    @Test(description = "增加频道")
    public void ADD_WEBSITE(String idx) throws Exception {
        //点击“频道接入”菜单
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_SITE_MENU);
        //增加频道
        MyUnion myUnion = new MyUnion(driver,action);
        if(!action.isElementExsit(By.id("submit_btn"))){
            myUnion.addWebSite();
        }
        WebSiteForm webSiteForm = new WebSiteForm(driver,action);
        webSiteForm.saveSite(InsDataService.getWebSiteById(idx));
        action.pause(1000l);
        //填写form表单
        action.assertStringTrue("模板列表页");
    }

    @Parameters({"idx"})
    @Test(description = "修改频道")
    public void MODIFY_WEBSITE(String idx) throws Exception{
        //点击“频道接入”菜单
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_SITE_MENU);
        WebSiteBean bean = InsDataService.getWebSiteById(idx);
        //在网站列表中找到网站并更新
        WebSiteTable webSiteTable = new WebSiteTable(driver,action);
        String status = webSiteTable.getStatus(bean.getWebName());
        if(StringUtils.isNotBlank(status)&&"待审核".equals(status)){
            webSiteTable.linkModify(bean.getWebName());
            WebSiteForm webSiteForm = new WebSiteForm(driver,action);
            bean.setPlatformType_pc("N");
            bean.setPlatformType_mobile("Y");
            bean.setWebIntro("修改网站-测试");
            webSiteForm.saveSite(bean);
            action.pause(1000l);
//            menuList.openMenu(Constants.U_F_SITE_MENU);
            action.assertStringTrue("修改网站-测试");
        }
    }

    @Parameters({"idx"})
    @Test(description = "删除频道")
    public void DEL_WEBSITE(String idx) throws Exception{
        //点击“频道接入”菜单
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_SITE_MENU);
        //在网站列表中找到网站并删除
        WebSiteBean bean = InsDataService.getWebSiteById(idx);
        WebSiteTable webSiteTable = new WebSiteTable(driver,action);
        String status = webSiteTable.getStatus(bean.getWebName());
        if(StringUtils.isNotBlank(status)&&"待审核".equals(status)){
            webSiteTable.linkDel(bean.getWebName());
            if(!action.isElementExsit(By.id("submit_btn"))){
                action.fail("删除网站失败");
            }
        }
    }

    @Task(taskName = {"DEL_API_CHANNEL.sql"},paras = {"param0=cimon001"})
    @Parameters({"idx"})
    @Test(description = "新增API")
    public void ADD_API(String idx) throws Exception{
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_API_MENU);
        MyUnion myUnion = new MyUnion(driver,action);
        myUnion.linkAddApi();
        ApiForm apiForm = new ApiForm(driver,action);
        apiForm.saveApi(InsDataService.getApiById(idx));
        action.pause(1000l);
        Assert.assertTrue(action.isElementExsit(By.xpath("//img[@class='api-success-cont']")));
    }

    @Parameters({"idx"})
    @Test(description = "修改API")
    public void MODIFY_API(String idx) throws Exception{
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_API_MENU);
        ApiBean ab = InsDataService.getApiById(idx);
        ApiTable apiTable = new ApiTable(driver,action);
        apiTable.linkModify(ab.getContactName());
        ab.setEmail("test@cc.com");
        ApiForm apiForm = new ApiForm(driver,action);
        apiForm.modifyApi(ab);
        action.pause(1000l);
        action.assertStringTrue("test@cc.com");
    }

    @Parameters({"idx"})
    @Test(description = "删除API")
    public void DEL_API(String idx) throws Exception {
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_API_MENU);
        ApiBean ab = InsDataService.getApiById(idx);
        ApiTable apiTable = new ApiTable(driver,action);
        apiTable.linkDel(ab.getContactName());
        action.pause(1000l);
        action.assertStringTrue("新增API");
    }

}
